package com.orcamento.controller;

import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.orcamento.csv.MinhasEconomiasCsv;
import com.orcamento.entity.model.Download;
import com.orcamento.entity.Fund;
import com.orcamento.service.FundService;


@SuppressWarnings("serial")
public class ExportController extends AbstractController {

	private static final Log LOGGER = LogFactory.getLog(ExportController.class);
	
	@Autowired
	private transient FundService fundService;
	
	private Download download;
	
	private Fund fund;
	
	
	public String minhasEconomias() {
		try {
			fund = fundService.findById(fund.getId(), getAccountId());
			MinhasEconomiasCsv minhasEconomiasCsv = new MinhasEconomiasCsv(fund.getGains(), fund.getSpents());
			download = new Download( minhasEconomiasCsv.toString(), fund.getEvent().getName() ); //Num futuro validar nomes de fundos com caracteres proibidos pelo windows
			return SUCCESS;
		} catch (Exception e) {
			LOGGER.warn("Erro ao exportar fundo para Minhas Economias.", e);
			return ERROR;
		}
	}

	public Fund getFund() {
		return fund;
	}

	public void setFund(Fund fund) {
		this.fund = fund;
	}
	
	public InputStream getDownload() throws Exception {
		return download.getStream();
	}
	
	public String getFilename() {
		return download.getFilename();
	}

}
